package com.kobeliu.controller;


import com.kobeliu.enums.DealStatus;
import com.kobeliu.pojo.bo.QueryReportBO;
import com.kobeliu.pojo.mo.ReportMO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.service.ReportService;
import com.kobeliu.utils.LocalDateUtils;
import com.kobeliu.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("report")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @PostMapping("create")
    public GraceJSONResult create(@RequestBody @Valid ReportMO mo){

        //校验
        boolean isExist = reportService.isReportRecordExist(mo.getReportUserId(), mo.getJobId());
        if(isExist)
            return GraceJSONResult.errorMsg("您已经举报过了");

        reportService.saveReportRecord(mo);
        return GraceJSONResult.ok();
    }


//    pagedReportRecordList
    @PostMapping("pagedReportRecordList")
    public GraceJSONResult create(@RequestBody @Valid QueryReportBO bo) {


        if(bo.getPage()== null ) bo.setPage(1);
        if(bo.getLimit() == null) bo.setLimit(10);

        String beginDate = bo.getBeginDate();
        String endDate = bo.getEndDate();
        if(beginDate != null) {
            //开始日期加上时间 00:00:00
            String beginStr = beginDate+ " 00:00:00";
//            String beginStr = LocalDateUtils.format(beginDate, LocalDateUtils.DATE_PATTERN)+ " 00:00:00";
//            LocalDateTime time = LocalDateUtils.parseLocalDateTime(beginStr, LocalDateUtils.DATETIME_PATTERN);
            bo.setBeginDate(beginStr);

        }
        if(endDate != null) {
            //结束时间加上23:59:59
            //String endStr = LocalDateUtils.format(endDate, LocalDateUtils.DATE_PATTERN)+ " 23:59:59";
            String endStr = endDate+ " 23:59:59";
//            LocalDateTime time = LocalDateUtils.parseLocalDateTime(endStr, LocalDateUtils.DATETIME_PATTERN);
            bo.setEndDate(endStr);
        }
        PagedGridResult result = reportService.findReportPage(bo);
        return GraceJSONResult.ok(result);
    }

    @PostMapping("deal/delete")
    public GraceJSONResult delete(String reportId) {
        reportService.updateReportRecordStatus(reportId, DealStatus.DONE);
        return GraceJSONResult.ok();
    }

    @PostMapping("deal/ignore")
    public GraceJSONResult ignore(String reportId) {
        reportService.updateReportRecordStatus(reportId, DealStatus.IGNORE);
        return GraceJSONResult.ok();
    }

}
